home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / CHRSWAP.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  2KB  |  57 lines

  1. /*********
  2. *  Function: CHRSWAP
  3. *  By: Tom Rettig
  4. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  5. *
  6. *  Syntax: CHRSWAP( <expC>, <char1> [,<char2>] )
  7. *  Return: <expC> with all occurrences of <char1> replaced with <char2>.
  8. *  Note  : <char1> and <char2> are both <expC>.
  9. *          <char1>s are simply removed if <char2> is null or omitted.
  10. *********/
  11.  
  12. #include "trlib.h"
  13.  
  14. TRTYPE chrswap()
  15. {
  16.    static char funcname[] = { "chrswap" };
  17.    char *instr, *replace, *newchar, *ret;
  18.    int i, j;
  19.  
  20.    if ( (PCOUNT==3 && ISCHAR(1) && ISCHAR(2) && ISCHAR(3)) ||
  21.         (PCOUNT==2 && ISCHAR(1) && ISCHAR(2))               )
  22.    {
  23.       instr   = _parc(1);
  24.       replace = _parc(2);
  25.       ret     = _tr_allocmem( (unsigned)(_tr_strlen(instr)+1));
  26.  
  27.       if ( ret )
  28.       {
  29.          if ( PCOUNT==3 )
  30.          {
  31.             newchar = _parc(3);
  32.             if (! *newchar)
  33.                goto noparam;
  34.             for ( i=0; instr[i]; i++ )
  35.                ret[i] = (instr[i]==replace[0]) ? newchar[0] : instr[i];
  36.          }
  37.          else     /* no replacement char */
  38.          {
  39. noparam:
  40.             for ( i=0, j=0; instr[j]; j++)
  41.             {
  42.                if ( instr[j]!=replace[0] )
  43.                   ret[i++] = instr[j];
  44.             }
  45.          }
  46.          ret[i] = NULLC;
  47.  
  48.          _retc( ret );
  49.          _tr_freemem( ret,(unsigned)(_tr_strlen(instr)+1) );
  50.       }
  51.       else
  52.          _retc( _tr_errmsgs(funcname,E_ALLOC) );
  53.    }
  54.    else
  55.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  56. }
  57.